package com.tiger.communal.service.impl;

import java.util.List;

import com.tiger.common.exception.CustomException;
import com.tiger.common.utils.DateUtils;
import com.tiger.common.utils.StringUtils;
import com.tiger.communal.domain.ConstructionUnit;
import com.tiger.communal.domain.ConstructionUnitEnclosure;
import com.tiger.communal.mapper.ConstructionUnitEnclosureMapper;
import com.tiger.communal.mapper.ConstructionUnitMapper;
import com.tiger.communal.service.IConstructionUnitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author tiger
 * @date 2022-09-13
 */
@Service
public class ConstructionUnitServiceImpl implements IConstructionUnitService {
    @Autowired
    private ConstructionUnitMapper constructionUnitMapper;
    @Autowired
    private ConstructionUnitEnclosureMapper enclosureMapper;

    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public ConstructionUnit selectConstructionUnitById(Long id) {
        ConstructionUnit unit = constructionUnitMapper.selectConstructionUnitById(id);
        //附件
        ConstructionUnitEnclosure unitEnclosure = new ConstructionUnitEnclosure();
        unitEnclosure.setUnitId(id);
        List<ConstructionUnitEnclosure> constructionUnitEnclosures = enclosureMapper.selectConstructionUnitEnclosureList(unitEnclosure);
        if (constructionUnitEnclosures != null && constructionUnitEnclosures.size() > 0){
            unit.getUnitEnclosureList().addAll(constructionUnitEnclosures);
        }
        return unit;
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param constructionUnit 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<ConstructionUnit> selectConstructionUnitList(ConstructionUnit constructionUnit) {
        return constructionUnitMapper.selectConstructionUnitList(constructionUnit);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param constructionUnit 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertConstructionUnit(ConstructionUnit constructionUnit) {
        constructionUnit.setCreatedAt(DateUtils.getNowDate());
        constructionUnit.setStatus(1L);//未提交
        constructionUnit.setIsDeleted(0);
        int i = constructionUnitMapper.insertConstructionUnit(constructionUnit);
        change(constructionUnit);
        return i;
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param constructionUnit 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateConstructionUnit(ConstructionUnit constructionUnit) {
        change(constructionUnit);
        constructionUnit.setUpdatedAt(DateUtils.getNowDate());
        return constructionUnitMapper.updateConstructionUnit(constructionUnit);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteConstructionUnitByIds(Long[] ids) {
        return constructionUnitMapper.deleteConstructionUnitByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteConstructionUnitById(Long id) {
        return constructionUnitMapper.deleteConstructionUnitById(id);
    }

    @Override
    public int callBack(Long id,Long status,String oaFile) {
        ConstructionUnit constructionUnit = constructionUnitMapper.selectConstructionUnitById(id);
        constructionUnit.setStatus(status);
        constructionUnit.setUpdatedAt(DateUtils.getNowDate());
        constructionUnit.setOaFile(oaFile);
        return constructionUnitMapper.updateConstructionUnit(constructionUnit);
    }


    private void change(ConstructionUnit constructionUnit){
        //附件
        enclosureMapper.deleteConstructionUnitEnclosureByUnitId(constructionUnit.getId());
        if (StringUtils.isNotEmpty(constructionUnit.getUnitEnclosureList())){
            constructionUnit.getUnitEnclosureList().forEach(item ->{
                item.setUnitId(constructionUnit.getId());
                item.setCreatedAt(DateUtils.getNowDate());
                enclosureMapper.insertConstructionUnitEnclosure(item);
            });
        }
    }




}
